<?
load_model('Common');
class Article extends Common {
	protected $_name ="article";
	protected $_primary = 'art_id';
	
	public function get_article_list(){
		$arr = $this->fetchAll(null,"art_id desc")->toArray();
		return $arr;
	}
	/**
	 * 取出所有article并相应的栏目名称
	 *@param $limit 限制范围
	 * @param $order 排序
	 * @return unknown
	 */
	public function get_article_lists($where = '', $order = 'order by a.art_id desc',$limit = ''){
		$sql = "select a.*,b.class_name from zf_article as a left join zf_class as b on a.art_classid=b.class_id $where $order $limit";
		$db = $this->getAdapter();
		return $db->fetchAll($sql);
	}
	/**
	 * 根据分类取出其分类下的所有文章
	 */
	public function get_article_by_class($class_id){
		$arr = $this->fetchAll("art_classid=$class_id","art_id desc")->toArray();
		return $arr;
	}
	
	public function get_article_info($id){
		$db = $this->getAdapter();
		$where = $db->quoteInto('art_id = ?', $id);
		return $this->fetchRow($where)->toArray();
	}
	
	public function add_art_tag($tag_ids,$art_id){
		if(!is_array($tag_ids)){
			return false;
		}
		$db = $this->getAdapter();
		foreach ($tag_ids as $tag_id){
			$sql = "insert into zf_tag_art (ta_tag_id,ta_art_id) values ('$tag_id','$art_id') ";
			$db -> query($sql);		
		}
		return true;
	}
	/**
	 * 获取所有tag_id根据where art_id=..
	 * 
	 * @param int $art_id
	 * @return tag_id结果数组
	 */
	function get_tagIds_by_artId($art_id){
		$db = $this->getAdapter();
		$select = $db->select();
		$select->where('ta_art_id = ?', $art_id);
		$select->from('zf_tag_art','ta_tag_id');
		$select->order('ta_tag_id asc');
		$arr = $db->fetchCol($select);
		return $arr;
	}
	/**
	 * 获取所有tag_name根据where art_id=..
	 * 
	 * @param int $art_id
	 * @return tag_name结果数组
	 */
	function get_tagNames_by_artId($art_id){
		$db = $this->getAdapter();
		$tag_ids = $this->get_tagIds_by_artId($art_id);
		$tag_names = array();
		foreach ($tag_ids as $row){
			$sql = "select tag_name from zf_tag where tag_id='$row' ";
			$tag_names[] = $db->fetchOne($sql);
		}
		return $tag_names;
	}
	
	function add_article($data){
		$dataInsert = array(
		    'art_title' => $data['art_title'],
		    'art_classid' => $data['art_classid'],
		    'art_keyword' => $data['art_keyword'],
		    'art_content' => $data['art_content'],
		    'art_put_time' => $data['art_put_time'],
		    'art_count' => $data['art_count'],
		);
		return $this->insert($dataInsert);
	}
	
	/**
	 * 更新文章与标签对应
	 *
	 * @param array $tag_ids 编辑中所有tag的id
	 * @param int $art_id
	 * @return unknown
	 */
	public function update_art_tag($tag_ids,$art_id){
		$db = $this->getAdapter();
		//过滤已经存在的映射
		$tag_id_list = $this->get_tagIds_by_artId($art_id);
		$new_tag_ids = array();
		foreach ($tag_ids as $row){
			if(!in_array($row, $tag_id_list)){
				$new_tag_ids[] = $row;
			}
		}
		foreach ($tag_id_list as $row){//删除垃圾映射
			if(!in_array($row, $tag_ids)){
				$sql = "delete from zf_tag_art where ta_tag_id='$row' ";
				$db -> query($sql);
			}
		}
		foreach ($new_tag_ids as $tag_id){
			$sql = "insert into zf_tag_art (ta_tag_id,ta_art_id) values ('$tag_id','$art_id') ";
			$db -> query($sql);		
		}
		return true;
		
	}
	/**
	 * 更新点击数
	 *
	 * @param unknown_type $data
	 * @return unknown
	 */
	public function update_count($id){
		$db = $this->getAdapter();
		$sql = "update zf_article set art_count=art_count+1 where art_id=$id";
		$db -> query($sql);
	}
	/**
	 * 更新评论数
	 *
	 * @param int $ar_id 文章id
	 * @param string $action 动作 删除/增加
	 * @param int $count 增加或删除个数
	 * @return unknown
	 */
	
	public function update_com_count($art_id,$action = 'add',$count = 1){
		$db = $this->getAdapter();
		switch($action){
			case 'add':
				$sql = "update zf_article set art_com_count=art_com_count+$count where art_id=$art_id";
				break;
			case 'del':
				$sql = "update zf_article set art_com_count=art_com_count-$count where art_id=$art_id";
				break;
			default:
				return false;
				break;
		}	
		$db -> query($sql);
	}
	function update_article($id, $data){
		$db = $this->getAdapter();
		$where = $db->quoteInto('art_id = ?', $id);
		return $this->update($data, $where);
	}
	
	function del_article($id){
		$db = $this->getAdapter();
		$where = $db->quoteInto('art_id = ?', $id);
		//删除文章对应标签
		load_model('Tag','include');
		$tag = new Tag();
		$tag->del_tag_by_artid($id);
		return $this->delete($where);
	}
	
}

?>